﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    //在线咨询
    public class CompanyOnlineDataPump : ICompanyOnlineAccess
    {
        public List<DefaultDataPack> GetOnlineConsultation(string copname, int page, int size)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_message
                              where c.c_vc_cpname == copname && !c.c_b_isdelete
                              orderby c.c_i_id descending
                              select new
                              {
                                  c.c_dt_pbdate,
                                  c.c_nvc_author,
                                  c.c_nvc_content,
                                  c.c_nvc_phone,
                                  c.c_nvc_reply,
                                  c.c_nvc_respondents,
                                  c.c_i_id,
                                  c.c_b_isReply
                              }).Skip(page * size).Take(size).ToList();
                return result.Select(c => new DefaultDataPack()
                        {
                            DATE = c.c_dt_pbdate.ToLongDateString(),
                            ID = c.c_i_id,
                            CONTENT = c.c_nvc_content,
                            PHONE = c.c_nvc_phone,
                            EXTFIELD1 = c.c_nvc_reply,
                            EXTFIELD2 = c.c_nvc_respondents,
                            AUTHOR = c.c_nvc_author,
                            ISREPLY= c.c_b_isReply
                        }).ToList();
            }
        }

        public List<DefaultDataPack> GetOnlineConsultationForIndex(string copname ,int size)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_message
                              where c.c_vc_cpname == copname && !c.c_b_isdelete && c.c_b_isReply
                              orderby c.c_i_id descending
                              select new
                              {
                                  c.c_dt_pbdate,
                                  c.c_nvc_author,
                                  c.c_nvc_content,
                                  c.c_nvc_phone,
                                  c.c_nvc_reply,
                                  c.c_nvc_respondents,
                                  c.c_i_id,
                                  c.c_b_isReply
                              }).Take(size).ToList();
                int itemCount = result.Count;
                if (itemCount < 3)
                {
                    result = result.Concat(
                        (from c in ctx.tb_message
                         where c.c_vc_cpname == copname && !c.c_b_isdelete
                         orderby c.c_i_id descending
                         select new
                         {
                             c.c_dt_pbdate,
                             c.c_nvc_author,
                             c.c_nvc_content,
                             c.c_nvc_phone,
                             c.c_nvc_reply,
                             c.c_nvc_respondents,
                             c.c_i_id,
                             c.c_b_isReply
                         }).Take(3 - itemCount).ToList()).ToList();
                }
                return result.Select(c => new DefaultDataPack()
                {
                    DATE = c.c_dt_pbdate.ToLongDateString(),
                    ID = c.c_i_id,
                    CONTENT = c.c_nvc_content,
                    PHONE = c.c_nvc_phone,
                    EXTFIELD1 = c.c_nvc_reply,
                    EXTFIELD2 = c.c_nvc_respondents,
                    AUTHOR = c.c_nvc_author,
                    ISREPLY = c.c_b_isReply
                }).ToList();
            }
        }

        public int GetOnlineConsulattionItemCount(string copname)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                return ctx.tb_message.Count(c => c.c_vc_cpname == copname && !c.c_b_isdelete);
            }
        }

        public int PostOnlineConsultation(string copname, string Author, string Content)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                ctx.AddTotb_message(new tb_message()
                {
                    c_b_isdelete = false,
                    c_dt_pbdate = DateTime.Now,
                    c_nvc_author = Author,
                    c_nvc_content = Content,
                    c_vc_cpname = copname
                });
                return ctx.SaveChanges();
            }
        }

        public int PostOnlineReply(string copname, int mid, string author, string content, string phone)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_message
                              where c.c_vc_cpname == copname && c.c_i_id == mid
                              select c).FirstOrDefault();
                if (result != null)
                {
                    result.c_nvc_respondents = author;
                    result.c_nvc_reply = content;
                    result.c_nvc_phone = phone;
                    result.c_b_isReply = true;
                    return ctx.SaveChanges();
                }
                return 0;
            }
        }

        public int DeleteOnlineItem(string copname, int mid)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var item = (from c in ctx.tb_message
                            where c.c_vc_cpname == copname && c.c_i_id == mid
                            select c).FirstOrDefault();
                if (item != null)
                {
                    item.c_b_isdelete = true;
                    return ctx.SaveChanges();
                }
            }
            return 0;
        }

        public List<DefaultDataPack> GetOnlineConsultationItem(string copname, int mid)
        {
            List<DefaultDataPack> list = new List<DefaultDataPack>();
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_message
                              where c.c_vc_cpname == copname && !c.c_b_isdelete && c.c_i_id ==mid
                              orderby c.c_i_id descending
                              select new
                              {
                                  c.c_dt_pbdate,
                                  c.c_nvc_author,
                                  c.c_nvc_content,
                                  c.c_nvc_phone,
                                  c.c_nvc_reply,
                                  c.c_nvc_respondents,
                                  c.c_i_id,
                                  c.c_b_isReply
                              }).FirstOrDefault();
                if (result != null)
                {
                    list.Add(new DefaultDataPack() {
                        DATE = result.c_dt_pbdate.ToLongDateString(),
                        ID = result.c_i_id,
                        CONTENT = result.c_nvc_content,
                        PHONE = result.c_nvc_phone,
                        EXTFIELD1 = result.c_nvc_reply,
                        EXTFIELD2 = result.c_nvc_respondents,
                        AUTHOR = result.c_nvc_author,
                        ISREPLY = result.c_b_isReply
                    });
                }
                return list;
            }
        }


        public bool EditOnlineConsultaion(string copname, OnlineDataPack onlineDataPack)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_message
                              where c.c_vc_cpname == copname && c.c_i_id == onlineDataPack.Mid
                              select c).FirstOrDefault();
                if (result != null)
                {
                    result.c_nvc_author = onlineDataPack.Author;
                    result.c_nvc_content = onlineDataPack.Content;
                    result.c_nvc_respondents = onlineDataPack.Respondent;
                    result.c_nvc_phone = onlineDataPack.PHONE;
                    result.c_nvc_reply = onlineDataPack.Reply;
                    result.c_b_isReply = true;
                }
                return ctx.SaveChanges() == 1;
            }
            return false;
        }
    }
}
